package com.badibadi.uniclubber;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.badibadi.activity.BaseActivity;
import com.badibadi.mytools.HtmlUtils;
import com.badibadi.mytools.MD5;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class ImageGetterTestActivity extends Activity {  
  
    private static final String TAG = "ImageGetterTestActivity";  
    /**本地图片*/  
    private TextView mTvOne;  
    /**项目资源图片*/  
    private TextView mTvTwo;  
    /**网络图片*/  
    private TextView mTvThree;  
    /**网络图片name*/  
//    private String picName = "set.jpg";
    /**网络图片Getter*/  
    private NetworkImageGetter mImageGetter; 
    /**网络图片路径*/  
//    private String htmlThree = "网络图片测试：" + "<img src='http://img.my.csdn.net/uploads/201307/14/1373780364_7576.jpg'>";  
    private String htmlThree="<img  type='face' title='ala囧' alt='ala囧' src='http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/b3/altjiong_thumb.gif' /><img  type='face' title='ala囧' alt='ala囧' src='http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/b3/altjiong_thumb.gif' /><img  type='face' title='ala囧' alt='ala囧' src='http://img.t.sinajs.cn/t35/style/images/common/face/ext/normal/b3/altjiong_thumb.gif' />"; 
   
    EditText et;
    
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_image_getter_test);  
          
        mTvOne = (TextView) this.findViewById(R.id.tv_img_label_one);  
        String htmlOne = "本地图片测试：" + "<img src='/mnt/sdcard/IMG0001A.jpg'>";  
        mTvOne.setText(Html.fromHtml(htmlOne, new LocalImageGetter(), null));  
  
        mTvTwo = (TextView) this.findViewById(R.id.tv_img_label_two);  
        String htmlTwo = "项目图片测试：" + "<img src=\""+R.drawable.ic_launcher+"\">";  
        mTvTwo.setText(Html.fromHtml(htmlTwo, new ProImageGetter(), null));  
       
          
        mTvThree = (TextView) this.findViewById(R.id.tv_img_label_three);  
//        mImageGetter = new NetworkImageGetter();  
//        mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));  
        HtmlUtils h=new HtmlUtils(ImageGetterTestActivity.this,mTvThree, htmlThree);
        h.showTextView();
        
        et = (EditText) this.findViewById(R.id.et);  
        new HtmlUtils(ImageGetterTestActivity.this,et, htmlThree).showEditText();
       
        
    
    }  
    /** 
     * 本地图片 
     * @author Susie 
     */  
    private final class LocalImageGetter implements Html.ImageGetter{  
          
        @Override  
        public Drawable getDrawable(String source) {  
            // 获取本地图片  
            Drawable drawable = Drawable.createFromPath(source);  
            // 必须设为图片的边际,不然TextView显示不出图片  
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
            // 将其返回  
            return drawable;  
        }  
    }  
    /** 
     * 项目资源图片 
     * @author Susie 
     */  
    private final class ProImageGetter implements Html.ImageGetter{  
  
        @Override  
        public Drawable getDrawable(String source) {  
            // 获取到资源id  
            int id = Integer.parseInt(source);  
            Drawable drawable = getResources().getDrawable(id);  
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
            return drawable;  
        }  
    }  
    /** 
     * 网络图片 
     * @author Susie 
     */  
    private final class NetworkImageGetter implements Html.ImageGetter{  
  
        @Override  
        public Drawable getDrawable(String source) {  
            Drawable drawable = null;  
            
            String path=MD5.getMD5(source);
            // 封装路径  
            File file = new File(Environment.getExternalStorageDirectory(), path); 
            // 判断是否以http开头  
            if(source.startsWith("http")) {  
                // 判断路径是否存在  
                if(file.exists()) {  
                    // 存在即获取drawable  
                    drawable = Drawable.createFromPath(file.getAbsolutePath());  
//                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
                } else {  
                    // 不存在即开启异步任务加载网络图片  
                    AsyncLoadNetworkPic networkPic = new AsyncLoadNetworkPic();
                    networkPic.execute(source);
                }  
            }  
            return drawable;  
        }  
    }  
    /** 
     * 加载网络图片异步类 
     * @author Susie 
     */  
    private final class AsyncLoadNetworkPic extends AsyncTask<String, Integer, Void>{  
  
        @Override  
        protected Void doInBackground(String... params) {  
            // 加载网络图片  
            loadNetPic(params);  
            return null;  
        }  
          
        @Override  
        protected void onPostExecute(Void result) {  
            super.onPostExecute(result);  
            // 当执行完成后再次为其设置一次  
            mTvThree.setText(Html.fromHtml(htmlThree, mImageGetter, null));  
        }  
        /**加载网络图片*/  
        private void loadNetPic(String... params) {  
            String path = params[0];  
              
            File file = new File(Environment.getExternalStorageDirectory(), MD5.getMD5(path));  
              
            InputStream in = null;  
              
            FileOutputStream out = null;  
              
            try {  
                URL url = new URL(path);

            HttpURLConnection connUrl = (HttpURLConnection) url.openConnection();  
                  
                connUrl.setConnectTimeout(5000);  
                  
                connUrl.setRequestMethod("GET");  
                  
                if(connUrl.getResponseCode() == 200) {  
                      
                    in = connUrl.getInputStream();  
                      
                    out = new FileOutputStream(file);  
                      
                    byte[] buffer = new byte[1024];  
                      
                    int len;  
                      
                    while((len = in.read(buffer))!= -1){  
                        out.write(buffer, 0, len);  
                    }  
                } else {
                }  
            } catch (Exception e) {  
                     
            } finally {  
                  
                if(in != null) {  
                    try {  
                        in.close();  
                    } catch (IOException e) {  
                             
                    }  
                }  
                if(out != null) {  
                    try {  
                        out.close();  
         } catch (IOException e) {  
                             
                    }  
                }  
            }  
        }  
    }  
}  

